package com.qlk.vorder.controller;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qlk.vorder.controller.base.BaseController;
import com.qlk.vorder.domain.SerialLog;
import com.qlk.vorder.domain.query.SerialLogQuery;
import com.qlk.vorder.service.SerialLogService;
import com.qlk.vorder.utils.exceptions.ExistedException;
import com.qlk.common.utils.page.PageUtil;
import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;

/**
 * SerialLogController ：日志流水控制器
 * 
 * @author zhaoyanan
 * @version 2016-7-28 14:14:00
 */
@Controller
@RequestMapping("serialLog")
public class SerialLogController extends BaseController {

	@Autowired
	private SerialLogService serialLogService;

	/** 视图前缀 */
	private static final String viewPrefix = "serialLog";

	private static final Log LOG = LogFactory.getLog(SerialLogController.class);

	/**
	 * 分页 查询数据
	 * 
	 * @param model
	 * @param page
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "")
	public String index(Model model, PageUtil page, SerialLogQuery query) {
		try {
			List<SerialLog> dataList = serialLogService.querySerialLogListWithPage(query, page);
			model.addAttribute("dataList", dataList);// 数据集合
			model.addAttribute("query", query);// 查询参数
			model.addAttribute("page", page);// 分页
		} catch (Exception e) {
			LOG.error("serialLog index has error.", e);
		}
		return viewPrefix + "/index";
	}

	/**
	 * 日志流水----添加跳转
	 * 
	 * @return
	 */
	@RequestMapping(value = "addForward")
	public String addForward() {
		return viewPrefix + "/add";
	}

	/**
	 * 日志流水----添加
	 * 
	 * @param serialLog
	 * @return
	 */
	@RequestMapping(value = "add")
	@ResponseBody
	public Wrapper<?> add(SerialLog serialLog) {
		try {
			// serialLog.setCreateUser(getLoginUserCnName());
			if (serialLogService.insert(serialLog)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功！");
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败！");
			}
		} catch (ExistedException e) {
			LOG.warn("serialLog add fail, exist serialLog.");
			return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败，已经存在");
		} catch (Exception e) {
			LOG.error("serialLog add has error.", e);
			return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败！");
		}
	}

	/**
	 * 日志流水----更新跳转
	 * 
	 * @param model
	 * @param serialLog
	 * @return
	 */
	@RequestMapping(value = "updateForward")
	public String updateForward(Model model, SerialLog serialLog) {
		try {
			SerialLog serialLogResult = serialLogService.getSerialLogById(serialLog.getId());
			model.addAttribute("serialLog", serialLogResult);
		} catch (Exception e) {
			LOG.error("serialLog updateForward has error.", e);
		}
		return viewPrefix + "/update";
	}

	/**
	 * 日志流水----更新
	 * 
	 * @param model
	 * @param serialLog
	 * @return
	 */
	@RequestMapping(value = "update")
	@ResponseBody
	public Wrapper<?> update(Model model, SerialLog serialLog) {
		try {
			// serialLog.setUpdateUser(getLoginUserCnName());
			if (serialLogService.update(serialLog)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "更新成功！");
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "更新失败！");
			}
		} catch (Exception e) {
			LOG.error("serialLog update has error.", e);
			return WrapMapper.error();
		}
	}

	/**
	 * 日志流水----删除
	 * 
	 * @param serialLog
	 * @return
	 */
	@RequestMapping(value = "delete")
	@ResponseBody
	public Wrapper<?> delete(SerialLog serialLog) {
		try {
			// serialLog.setUpdateUser(getLoginUserCnName());
			if (serialLogService.delete(serialLog)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "删除成功！");
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "删除失败！");
			}
		} catch (Exception e) {
			LOG.error("serialLog delete has error.", e);
			return WrapMapper.error();
		}
	}

	/**
	 * 日志流水----查询-无分页
	 * 
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "query")
	@ResponseBody
	public Wrapper<?> query(SerialLogQuery query) {
		try {
			List<SerialLog> list = serialLogService.querySerialLogList(query);
			if (!CollectionUtils.isEmpty(list)) {
				return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, list);
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "查询配置信息失败！");
			}
		} catch (Exception e) {
			LOG.error("serialLog query has error.", e);
			return WrapMapper.error();
		}
	}

	/**
	 * 查询日志流水详情
	 * 
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "detail", method = RequestMethod.GET)
	@ResponseBody
	public Wrapper<?> detail(SerialLogQuery query) {
		if (null == query || null == query.getId()) {
			return illegalArgument();
		}

		try {
			SerialLog serialLog = serialLogService.getSerialLogById(query.getId());
			if (serialLog != null) {
				return new Wrapper<SerialLog>().result(serialLog);
			} else {
				return WrapMapper.wrap(Wrapper.ERROR_CODE, "查询日志流水详情失败！");
			}
		} catch (Exception e) {
			LOG.warn("detail serialLog has error.", e);
			return error();
		}
	}
}
